﻿using System;
using System.Data.OleDb;

namespace ADEVSO.Data.OleDb
{
    public class MSAccess
    {
        #region Constructors

        public MSAccess()
        {
        }

        public MSAccess(String ConnectionString)
        {
            this.ConnectionString = ConnectionString;
        }

        #endregion

        #region Variables

        private OleDbConnection dbConnection;
        private OleDbCommand dbCommand;
        private String dbConnectionString;

        #endregion

        #region Properties

        /// <summary>
        /// Database connectionString
        /// </summary>
        public String ConnectionString
        {
            set { dbConnectionString = value; }
            get { return dbConnectionString; }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Connect to Database
        /// </summary>
        /// <returns></returns>
        public String Connect()
        {
            try
            {
                dbConnection = new OleDbConnection(dbConnectionString);
                dbConnection.Open();
                return null;
            }
            catch (OleDbException e)
            {
                return e.Message;
            }
        }

        /// <summary>
        /// Connect to Database
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public String Connect(String ConnectionString)
        {
            this.ConnectionString = ConnectionString;
            return Connect();
        }

        /// <summary>
        /// Disconnect to Database
        /// </summary>
        /// <returns></returns>
        public String Disconnect()
        {
            try
            {
                dbConnection.Close();
                return null;
            }
            catch (OleDbException e)
            {
                return e.Message;
            }
        }

        /// <summary>
        /// Execute SqlCommand
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public OleDbDataReader Execute(String sql)
        {
            OleDbDataReader dbDataReader = null;
            try
            {
                dbCommand = new OleDbCommand(sql, dbConnection);
                dbDataReader = dbCommand.ExecuteReader();
            }
            //catch (OleDbException e)
            catch
            {
            }
            return dbDataReader;
        }

        /// <summary>
        /// ExecuteNonQuery SqlCommand
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int ExecuteNonQuery(String sql)
        {
            int result = 0;
            try
            {
                dbCommand = new OleDbCommand(sql, dbConnection);
                result = dbCommand.ExecuteNonQuery();
            }
            //catch (OleDbException e)
            catch
            {
            }
            return result;
        }

        #endregion
    }
}
